package com.sincebest.fivestar.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.math.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.sincebest.fivestar.common.CommandEnum;
import com.sincebest.fivestar.common.RobotCommand;
import com.sincebest.fivestar.common.exception.RobotErrors;
import com.sincebest.fivestar.common.utils.JsonTools;
import com.sincebest.fivestar.common.utils.RestClient;
import com.sincebest.fivestar.mapper.RobotMapper;
import com.sincebest.fivestar.model.GameResult;
import com.sincebest.fivestar.model.Robot;
import com.sincebest.fivestar.model.RobotWxGroupRela;
import com.sincebest.fivestar.service.GameService;

@Service
public class GameServiceImpl implements GameService{
	
	private static final Logger log = LoggerFactory.getLogger(GameServiceImpl.class);

	private static final String host = "https://console.tim.qq.com/";
	
	@Value("${im.usersig}")
	private String usersig;
	
	@Autowired
	private RobotMapper robotMapper;
	
	@Override
	public Boolean notifyGameResult(GameResult result) {
		RobotWxGroupRela rela = robotMapper.selectRobotWxGroupRelaByHouseId(result.getHouseId());
		if (rela != null){
			RobotCommand command = new RobotCommand();
			command.setCommand(CommandEnum.SEND_MSG);
			command.setTargetId(rela.getWeChatGroupId());
			command.setContent(JsonTools.simpleJson(result));
			Robot robot = robotMapper.getImIdById(rela.getRobotId());
			return sendMsg(result, robot.getImId());
		} else {
			throw RobotErrors.MAJ_HOUSE_NO_ROBOT.bornException();
		}

	}

	@Override
	public Boolean gameInvite(String houseId, String enterInviteUrl){
		RobotWxGroupRela rela = robotMapper.selectRobotWxGroupRelaByHouseId(houseId);
		if (rela != null){
			RobotCommand command = new RobotCommand();
			command.setCommand(CommandEnum.SEND_MSG);
			command.setTargetId(rela.getWeChatGroupId()); //给群发消息
			command.setContent(enterInviteUrl);
			
			Robot robot = robotMapper.getImIdById(rela.getRobotId());
			return sendMsg(command, robot.getImId());
		} else {
			throw RobotErrors.MAJ_HOUSE_NO_ROBOT.bornException();
		}

	}
	
	private Boolean sendMsg(Object content, String targetRobotId) {
		String path ="v4/openim/sendmsg";
		String urlParam = "usersig="+usersig+"&identifier=admin&sdkappid=1400027110&random=99999999&contenttype=json";
		Map<String, Object> msg = new HashMap();
		msg.put("SyncOtherMachine", 2);//消息不同步至发送方
		msg.put("To_Account", targetRobotId);
		msg.put("MsgRandom", RandomUtils.nextInt(9999999));
		msg.put("MsgTimeStamp", 1212312);
		Map<String, Object> bodyMsg = new HashMap();
		bodyMsg.put("MsgType", "TIMTextElem");
		Map<String, Object> contentMap = new HashMap();
		contentMap.put("Text", JsonTools.simpleJson(content)); //重要： 文本消息，一定要求有带Text的key
		bodyMsg.put("MsgContent", contentMap);
		List<Map> bodylist = new ArrayList();
		bodylist.add(bodyMsg);
		msg.put("MsgBody", bodylist);

		JSONObject resp = RestClient.post(host+path+"?"+urlParam, JsonTools.simpleJson(msg));
		Boolean result = resp.getString("ActionStatus").equals("OK");
		if (!result){
			log.error(resp.getString("ErrorInfo"));
		}
		return resp.getString("ActionStatus").equals("OK");
		
	}

}
